
#include "../../common/debug.h"
#include "../../common/opcodemgr.h"
#include "../../common/buildfile.h"
#include <string.h>
#include "versions.h"
//Must include all valid version headers
#include "patch_current.h"
#include "patch_Titanium.h"
#include "patch_SoF.h"

ExtractorAbstractFactory::ExtractorAbstractFactory(const char *opcodes_file) {
	opcodeManager = new RegularOpcodeManager();
	
	//we dont care how this turns out, worst case it falls
	//back to a NULL manager and dosent do anything.
	opcodeManager->LoadOpcodes(opcodes_file, false);
}

ExtractorAbstractFactory::~ExtractorAbstractFactory() {
	safe_delete(opcodeManager);
}

void ExtractorAbstractFactory::ListExtractorFactories() {
	printf("Avaliable Patches:\n"
		"\tcurrent\t- The current eqemu code (Live compat, however good it is)\n"
		"\tTitanium - The Titanium boxed edition (10/27/2005->12/06/2005)\n"
		"\tSoF - The SoF boxed edition\n"
		"\n"
	);
}

ExtractorAbstractFactory *ExtractorAbstractFactory::GetExtractorFactoryByName(const char *patch_name, const char *filename) {
	if(!strcasecmp(patch_name, "current")) {
		return(new EQE_Patch_Current::ExtractorConcreteFactory(filename));
	}
	if(!strcasecmp(patch_name, "Titanium")) {
		return(new EQE_Patch_Titanium::ExtractorConcreteFactory(filename));
	}
	if(!strcasecmp(patch_name, "SoF")) {
		return(new EQE_Patch_SoF::ExtractorConcreteFactory(filename));
	}
	return(NULL);
}

//second constants to make patch dates easier
#define YEAR_2004 1072935480
#define YEAR_2005 1104492410
#define YEAR_2007 1167606262
#define MONTH 2629743
#define DAY 86400

ExtractorAbstractFactory *ExtractorAbstractFactory::GetExtractorFactoryByDate(uint32 stamp, const char *filename) {
	//dated patches
//printf("date %d, 5/11=%d, 2/15=%d\n", stamp, YEAR_2005 + 6*MONTH + 10*DAY, YEAR_2005 + 4*MONTH + 10*DAY);

	if(stamp < (YEAR_2004 + 11*MONTH + 14*DAY)) {
		fprintf(stderr, "# Detected Patch before our known history. Unable to process.\n");
		return(NULL);
	} 
	else if(stamp < (YEAR_2005 + 11*MONTH + 6*DAY)) {
		// patch after 10-27-05 to 12-06-05
		fprintf(stderr, "# Auto-detected patch date of 10-27-05 (Titanium)\n");
		return(new EQE_Patch_Titanium::ExtractorConcreteFactory(filename));
	}
	else if(stamp < (YEAR_2007 + 11*MONTH + 6*DAY)) {
		// patch after 10-27-05 to 12-06-05
		fprintf(stderr, "# Auto-detected patch date of 10-27-05 (Titanium)\n");
		return(new EQE_Patch_SoF::ExtractorConcreteFactory(filename));
	}
	//current patch on: 
	fprintf(stderr, "# Time Stamp is after any known patch, using current (live compat) structs.\n");
	//if its not one of the past patches, assume its the most recent.
	return(new EQE_Patch_Current::ExtractorConcreteFactory(filename));
}






